//
//  ACHLHttpRequestTool.m
//  hostInfomationViewController
//
//  Created by imac on 16/3/31.
//  Copyright © 2016年 GuoXiaoji. All rights reserved.
//

#import "ACHLHttpRequestTool.h"
#import "AFNetworking.h"
#import "AppDelegate.h"

static ACHLHttpRequestTool *_httpRequestTool = nil;

@interface ACHLHttpRequestTool()

@property (nonatomic,strong)AFHTTPSessionManager *manager;
@property (nonatomic,strong) AFSecurityPolicy *securityPolicy;

@end


@implementation ACHLHttpRequestTool

+(instancetype)shareHttpRequestTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _httpRequestTool = [[self alloc]init];
    });
    
    return _httpRequestTool;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _httpRequestTool = [super allocWithZone:zone];
        
        _httpRequestTool.manager = [AFHTTPSessionManager manager];
        
        _httpRequestTool.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        _httpRequestTool.manager.requestSerializer.timeoutInterval = 10;
//        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"petnet" ofType:@"cer"];
//        NSData * certData =[NSData dataWithContentsOfFile:cerPath];
//        NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
//        _httpRequestTool.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//        // 设置证书
//        [_httpRequestTool.securityPolicy setPinnedCertificates:certSet];
//        // 不允许无效的证书
//        _httpRequestTool.securityPolicy.allowInvalidCertificates = YES;
//        _httpRequestTool.securityPolicy.validatesDomainName = YES;
//        _httpRequestTool.manager.securityPolicy = _httpRequestTool.securityPolicy;

    });

    return _httpRequestTool;
    
}

//默认提交请求的数据是二进制,返回格式是JSON

-(void)postWithURL:(NSString *)url params:(id)params success:(successBlock)success failure:(void (^)(NSError *))failure
{
//    AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    if (appDlg.isReach) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            
            NSNumber *code = dic[@"code"];
            
            NSString *message = dic[@"msg"];
            
            NSDictionary *data = dic[@"data"];
            
            if([code isEqualToNumber:@10003]){
                
//                [ACHLTool userLogoutClearAccountAndChangeLeftItemImage];
              
            }
            
            if(success){
                
                success(code,message,data);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if(failure){
                failure(error);
            }
            
        }];
            
        });
        
//    } else {
//        [MBProgressHUD showError:@"网络连接异常"];
//    }
    
}
-(void)getArrayWithURL:(NSString *)url params:(NSDictionary *)params successArray:(successArrayBlock)successArray failure:(void (^)(NSError *))failure
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
                
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                id  responseData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                
                if(successArray){
                    
                    successArray(responseData);
                }
                
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                if(failure){
                    
                    failure(error);
                }
                
                
            }];
    });

}

-(void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(successBlock)success failure:(void (^)(NSError *))failure
{
//    AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
//    if (appDlg.isReach) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            
            NSNumber *code = [NSNumber numberWithInteger:[dic[@"code"] integerValue]];
            NSString *message = dic[@"msg"];
            
            NSDictionary *data = dic[@"data"];
            
            //        if ([message isEqualToString:@"成功"] && !data) {
            //
            //            data = dic[@"data"];
            //
            success(code,message,data);
            //        }
            //
            
            if([code isEqualToNumber:@10003]){
                
//                [ACHLTool userLogoutClearAccountAndChangeLeftItemImage];
              
            }
            
            //        if(success){
            //
            //            success(code,message,data);
            //        }
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
            if(failure){
                
                failure(error);
            }
            
            success(nil, nil, error);
            
            
            if (error.code == -1009) {
//                [MBProgressHUD showError:@"网络未连接"];
            }
            
            
        }];

        
    });
    
    


}
-(void)postWithURL:(NSString *)url params:(NSDictionary *)params formData:(NSDictionary *)imageDataDic success:(successBlock)success failure:(void (^)(NSError *))failure
{
//    AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//    if (appDlg.isReach) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            for(NSString *key in imageDataDic){
                
                [formData appendPartWithFileData:imageDataDic[key] name:key fileName:[NSString stringWithFormat:@"%@.jpg",key] mimeType:@"image/jpeg"];
                
            }
            
            
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            
            NSNumber *code = dic[@"code"];
            
            NSString *message = dic[@"msg"];
            
            NSDictionary *data = dic[@"data"];
            
            if([code isEqualToNumber:@10003]){
                
//                [ACHLTool userLogoutClearAccountAndChangeLeftItemImage];
              
            }
            
            if(success){
                
                success(code,message,data);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            if(failure){
                failure(error);
            }
            
            
        }];
            
        });

//     } else {
//         [MBProgressHUD showError:@"网络连接异常"];
//    }
    
}

//// 宠物详情
//- (void)getWithPetUrl:(NSString *)url params:(NSDictionary *)params success:(petSuccessBlcok)success failure:(void (^)(NSError *error))failure {
//    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self.manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
//            
//            
//        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            
//            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//            
//            NSNumber *code = dic[@"code"];
//            
//            NSString *message = dic[@"msg"];
//            
//            NSDictionary *data = dic[@"data"];
//            
//            NSDictionary *healthy = dic[@"healthy"];
//            
//            //        if ([message isEqualToString:@"成功"] && !data) {
//            //
//            //            data = dic[@"data"];
//            //
//            success(code,message,data,healthy);
//            //        }
//            //
//            
//            if([code isEqualToNumber:@10003]){
//                
//                [ACHLTool userLogoutClearAccountAndChangeLeftItemImage];
//                
//            }
//            
//            //        if(success){
//            //
//            //            success(code,message,data);
//            //        }
//            
//            
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            
//            
//            if(failure){
//                
//                failure(error);
//            }
//            
//            success(nil, nil,nil, error);
//            
//            
//            if (error.code == -1009) {
//                [MBProgressHUD showError:@"网络未连接"];
//            }
//            
//        }];
//        
//    });
//    
//}


//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
@end
